#import "TileView.h"

@implementation TileView
-(id) initWithFrame: (CGRect)frame andLabel: (int)aLabel {
    if ((self = [super initWithFrame:frame])) {
        self.tag = aLabel;
    }
    return self;
}

-(void) drawRect: (CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
    CGFloat colors[] = { 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0 };
    CGPoint start = CGPointMake(0.0, 0.0);
	CGPoint end = CGPointMake(20.0, 20.0);
    end.y += rect.size.height;    
	CGGradientRef gradient = CGGradientCreateWithColorComponents (rgb, colors, NULL, 2);
    CGContextDrawLinearGradient (context, gradient, start, end, 0);
    CGContextSetStrokeColorSpace(context, rgb);
    const CGFloat stokeColor[] = {1.0, 1.0, 1.0, 1.0}; 
    CGContextSetStrokeColor(context, stokeColor);
    CGContextStrokeRect(context, rect);
    label = [[UILabel alloc] initWithFrame: rect];
    label.text = [NSString stringWithFormat:@"%d", self.tag + 1];
    label.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor colorWithWhite:0.0 alpha:1.0];
    label.font = [UIFont fontWithName:@"Verdana" size:32];
    [self addSubview:label];
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [AppDelegate moveTileWithIndex: self.tag];
}

-(void) dealloc {
    [label release];
    [super dealloc];
}
@end
